<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="templatelist" >
      <el-table-column label="模板id" align="center" prop="template_id" />
      <el-table-column label="版本号" align="center" prop="user_version" />
      <el-table-column label="版本描述" align="center" prop="user_desc" />
      <el-table-column label="上传时间" align="center" prop="create_time" />
      <el-table-column label="模板类型" align="center" prop="template_type" />
      <el-table-column label="appid" align="center" prop="source_miniprogram_appid" />
      <el-table-column label="小程序名称" align="center" prop="source_miniprogram" />
      <el-table-column label="审核状态" align="center" prop="audit_status" />
      <el-table-column label="审核驳回原因" align="center" prop="reason" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-upload"
            @click="handleUpload(scope.row)"
          >代码上传</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改优惠券对话框 -->
    <el-dialog title="代码上传" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="模板ID" prop="template_id">
              {{form.template_id}}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="小程序" prop="appid">
              <el-select v-model="form.appid"  placeholder="请选择" style="width: 100%">
                <el-option
                  v-for="item in appidOptions"
                  :key="item.appid"
                  :label="item.nickName"
                  :value="item.appid"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="代码版本号" prop="template_id">
              <el-input v-model="form.user_version" placeholder="请输入代码版本号" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="代码描述" prop="user_desc">
              <el-input v-model="form.user_desc" placeholder="请输入代码描述" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {deletetemplate, gettemplatelist, templateUpload} from "../../../api/wechat/wechat";
import {appletList} from "../../../api/wechat/applet";

export default {
  name: "appTemplate",
  data() {
    return {
      // 遮罩层
      loading: true,
      templatelist: [],
      appidOptions:[],
      open : false,
      form : {
        template_id : null,
        appid :null,
        user_version : null,
        user_desc : null
      },
      // 表单校验
      rules: {
        template_id: [
          {required: true, message: "模板ID不能为空", trigger: "blur"}
        ],
        appid: [
          {required: true, message: "小程序Id不能为空", trigger: "change"}
        ],
        user_version: [
          {required: true, message: "代码版本号不能为空", trigger: "change"}
        ],
        user_desc: [
          {required: true, message: "代码描述不能为空", trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getAppidList();
  },
  methods: {
    /** 查询小程序商户列表 */
    getList() {
      this.loading = true;
      gettemplatelist().then(response => {
        this.templatelist = response.data.template_list;
        this.loading = false;
      });
    },
    getAppidList() {
      appletList().then(response => {
        this.appidOptions = response.data;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.template_id;
      this.$confirm('是否确认删除ID"' + id + '"的模板?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return deletetemplate(id);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset()
    },
    // 表单重置
    reset() {
      this.form =  {
        template_id : null,
        appid :null,
        user_version : null,
        user_desc : null
      }
      this.resetForm("form");
    },
    handleUpload(row){
      this.reset()
      this.form.template_id = row.template_id;
      this.form.user_version = row.user_version;
      this.form.user_desc = row.user_desc;
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          templateUpload(this.form).then(res =>{
            if (res.data.errcode == 0) {
              this.msgSuccess("操作成功");
            }else {
              this.msgError("错误代码："+res.data.errcode+","+res.data.errmsg)
            }
            this.open = false;
          })
        }
      });
    },
  }
};
</script>
